package com.nttuan.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nttuan.flows.MultiSelectListener;
import com.nttuan.mycalender.R;

import java.util.ArrayList;

public class MultiSelect extends LinearLayout {

    private View mView;
    private Context mContext;
    private ArrayList<ViewItem> mList;
    private int COLOR_SELECT = 0;
    private int COLOR_DEFALT = 0;
    private boolean enable = true;
    private MultiSelectListener listener;

    public MultiSelect(Context context, AttributeSet attr) {
        super(context, attr);
        this.mContext = context;
        this.mList = new ArrayList<ViewItem>();

        this.mView = LayoutInflater.from(mContext).inflate(R.layout.mutil_select_component, null);
        this.addView(mView);
        init();
        setArrĐay(context.getResources().getStringArray(R.array.dayOfWeekShort));
    }

    public void setListener(MultiSelectListener listener) {
        this.listener = listener;
    }

    public void init() {
        COLOR_DEFALT = mContext.getResources().getColor(R.color.disable);
        COLOR_SELECT = mContext.getResources().getColor(R.color.enable);

        TextView tv = (TextView) mView.findViewById(R.id.toggle0);
        mList.add(new ViewItem(tv, false));
        tv = (TextView) mView.findViewById(R.id.toggle1);
        mList.add(new ViewItem(tv, false));
        tv = (TextView) mView.findViewById(R.id.toggle2);
        mList.add(new ViewItem(tv, false));
        tv = (TextView) mView.findViewById(R.id.toggle3);
        mList.add(new ViewItem(tv, false));
        tv = (TextView) mView.findViewById(R.id.toggle4);
        mList.add(new ViewItem(tv, false));
        tv = (TextView) mView.findViewById(R.id.toggle5);
        mList.add(new ViewItem(tv, false));
        tv = (TextView) mView.findViewById(R.id.toggle6);
        mList.add(new ViewItem(tv, false));
        tv = (TextView) mView.findViewById(R.id.toggleAll);
        mList.add(new ViewItem(tv, false));
        for (ViewItem item : mList)
            item.getView().setOnClickListener(clickAction);
    }

    public String getData() {
        String buff = "";
        for (int i = 0; i < mList.size() - 1; i++) {
            ViewItem item = mList.get(i);
            if (item.getSelect())
               buff+= (i + ",");
        }
        return buff;
    }

    public void setArrĐay(String[] arr) {
        for (int i = 0; i < mList.size(); i++)
            mList.get(i).getView().setText(arr[i]);
    }

    private void changeAll() {
        int color;
        Boolean isSelect;
        if (mList.get(mList.size() - 1).getSelect()) {
            color = COLOR_DEFALT;
            isSelect = false;
        } else {
            color = COLOR_SELECT;
            isSelect = true;
        }
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setSelect(isSelect);
            mList.get(i).getView().setTextColor(color);
        }

    }

    public void setData(String repeat) {
        if (repeat != null && !repeat.equals("")) {
            String[] arr = repeat.split(",");
            int index;
            ViewItem item;
            for (int i = 0; i < arr.length; i++) {
                index = Integer.parseInt(arr[i]);
                item = mList.get(index);
                item.setSelect(true);
                item.getView().setTextColor(COLOR_SELECT);
            }
        }
    }

    private void changeSelect(View v) {
        if (!enable)
            return;

        if (v == mList.get(mList.size() - 1).getView()) {
            changeAll();
            if(listener != null)
                listener.select(getData());
            return;
        }

        for (int i = 0; i < mList.size(); i++) {
            ViewItem item = mList.get(i);
            TextView tv = item.getView();
            if (v == tv) {
                if (item.getSelect()) {
                    tv.setTextColor(COLOR_DEFALT);
                    mList.get(i).setSelect(false);
                } else {
                    tv.setTextColor(COLOR_SELECT);
                    mList.get(i).setSelect(true);
                }
                if(listener != null)
                    listener.select(getData());
                return;
            }
        }
    }

    private OnClickListener clickAction = new OnClickListener() {
        @Override
        public void onClick(View v) {
            changeSelect(v);
        }
    };

    class ViewItem {
        private TextView view;
        private Boolean isSelect;

        public ViewItem(TextView v, Boolean isSelect) {
            this.view = v;
            this.isSelect = isSelect;
        }

        public void setSelect(Boolean isSelect) {
            this.isSelect = isSelect;
        }

        public TextView getView() {
            return this.view;
        }

        public Boolean getSelect() {
            return this.isSelect;
        }
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }
}
